<template>
  <card v-bind="cardOptions" @save="save">
    <y-form ref="$form" v-bind="options2"></y-form>
  </card>
</template>

<script>
export default {
  mounted() {
    var { keyValue, parentId } = this.$route.query;
    if (!!keyValue) {
      this.cardOptions.title = "菜单 / 编辑";
      this.initData(keyValue);
    } else {
      this.cardOptions.title = "菜单 / 新增";
      this.$refs.$form.setData({parentId : (parentId || "0")})
    }
  },
  data() {
    return {
      keyValue: "",
      cardOptions: {
        title: "",
        btns: ["save", "back"],
      },
      options2: {
        items: [
          {
            title: "菜单信息",
            fields: [
              {
                type: "input",
                field: "name",
                text: "名称",
                required: true,
                placeholder: "请输入名称",
              },
              { type: "input", field: "icon", text: "图标" },
              { type: "dataItem", field: "type", text: "类型",code:'MenuType'},
              { type: "cascader", field: "parentId", text: "父级",code:'MenuCascaders'},
            ],
          },
          {
            title: "路由信息",
            fields: [
              { type: "input", field: "routeName", text: "名称" },
              { type: "input", field: "routePath", text: "路径" },
              { type: "input", field: "routeRedirect", text: "重定向" },
              { type: "input", field: "routeComponent", text: "视图组件" },
            ],
          },
        ],
      },
    };
  },
  methods: {
    async initData(keyValue) {
      var { data } = await $y.api.system.menu.get(keyValue);
      this.$refs.$form.setData(data);
    },
    async save() {
      var formData = this.$refs.$form.getFormData();
      if(!formData.id){
        await this.$y.api.system.menu.insert([formData])
        $y.notifier.success("新增成功！");
      }else{
        await this.$y.api.system.menu.update([formData])
        $y.notifier.success("保存成功！");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
</style>